package niuke;

public class niuKeMain9 {
    public int findPoisonedDuration(int[] timeSeries, int duration) {
        if (timeSeries==null){
            return 0;
        }
        int sum=0;
        for (int i = 0; i < timeSeries.length-1; i++) {
            if ((timeSeries[i]+duration-1)>=timeSeries[i+1]){
                sum+=timeSeries[i+1]-timeSeries[i];
            }
            else {
                sum+=duration;
            }
        }
        sum+=duration;
        return sum;
    }
    public int[][] solve (int[][] a, int[][] b) {
        // write code here
        int arr[][]=new int[a.length][a[0].length];
        int m=a.length;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < m; j++) {
                int sum=0;
                for (int k = 0; k < m; k++) {
                    sum+=a[i][k]*b[k][j];
                }
                arr[i][j]=sum;
            }
        }
        return arr;
    }
    public int maxArea (int[] height) {
        // write code here
        if (height==null||height.length<2){
            return 0;
        }
        if (height.length==2){
            int min=Math.min(height[0],height[1]);
            return min*min;
        }
        int n=height.length;
        int left=0;
        int right=n-1;
        int val=0;
        int max=0;
        while (left<=right){
            val=(right-left)*Math.min(height[left],height[right]);
            max=Math.max(val,max);
            if (height[left]<=height[right]){
                left++;
            }else {
                right--;
            }
        }
        return max;
    }
    public boolean isValidBST (TreeNode root) {
        // write code here
        if(root==null){
            return true;
        }
        if(root.left!=null&&root.val<root.left.val){
            return false;
        }
        if(root.right!=null&&root.val>root.right.val){
            return false;
        }
        return isValidBST(root.left)&&isValidBST(root.right);
    }
    public int findMedianinTwoSortedAray (int[] arr1, int[] arr2) {
        // write code here
        if (arr1.length==1){
            return arr1[0];
        }
        int n= arr1.length;
        int mid=(arr1.length-1)/2;
        int count=0;
        for (int i = mid; i < n; i++) {
            arr1[count++]=arr1[mid];
        }
        count=count-1;
        for (int i = mid; i < n; i++) {
            if (arr1[count]!=arr2[i]){
                arr1[++count]=arr2[i];
            }
        }
        return arr1[arr1.length/2];
    }
    public ListNode swapLinkedPair (ListNode head) {
        // write code here
        if (head==null||head.next==null){
            return head;
        }
        ListNode node=head;
        int count=0;
        while (node!=null){
            count++;
            node= node.next;
        }
        ListNode[] n1=new ListNode[count];
        for (int i = 0; i < count; i++) {
            n1[i]=head;
            head=head.next;
        }
        int left=0;
        while (left<count-1){
            ListNode n2=n1[left];
            n1[left]=n1[left+1];
            n1[left+1]=n2;
            left+=2;
        }
        n1[count-1].next=null;
        for (int i = 0; i < count-1; i++) {
            n1[i].next=n1[i+1];
        }
        return n1[0];
    }
}
